import request from '../utils/require';

// 二手商品相关接口

/**
 * 发布二手商品
 */
export interface PublishItemParams {
  userId: string;
  title: string;
  description?: string;
  price: string;
  conditionLevel?: string;
  category?: string;
  images?: string;
}

export interface PublishItemResponse {
  success: boolean;
  message: string;
  data?: {
    itemId: number;
    title: string;
    price: number;
    conditionLevel: string;
    category: string;
  };
}

export function publishItem(params: PublishItemParams): Promise<PublishItemResponse> {
  return request.post('/api/item/publish', params);
}

/**
 * 获取用户发布的商品列表
 */
export interface UserItemsResponse {
  success: boolean;
  message: string;
  data?: {
    items: SecondHandItemSimple[];
    total: number;
  };
}

export interface SecondHandItemSimple {
  id: number;
  title: string;
  price: number;
  conditionLevel: string;
  category: string;
  status: string;
  viewCount: number;
}

export function getUserItems(userId: number): Promise<UserItemsResponse> {
  return request.get('/api/item/my-items', {
    params: { userId }
  });
}

/**
 * 获取商品列表（分页、搜索）
 */
export interface ItemsListParams {
  category?: string;
  keyword?: string;
  page?: number;
  size?: number;
}

export interface ItemsListResponse {
  success: boolean;
  message: string;
  data?: {
    total: number;
    items: SecondHandItemList[];
  };
}

export interface SecondHandItemList {
  id: number;
  title: string;
  price: number;
  conditionLevel: string;
  category: string;
  images: string;
  viewCount: number;
  status: string;
}

export function getItemsList(params?: ItemsListParams): Promise<ItemsListResponse> {
  return request.get('/api/item/list', {
    params: {
      category: params?.category,
      keyword: params?.keyword,
      page: params?.page || 1,
      size: params?.size || 10
    }
  });
}

/**
 * 获取商品详情
 */
export interface ItemDetailResponse {
  success: boolean;
  message?: string;
  data?: {
    id: number;
    userId: number;
    title: string;
    description: string;
    price: number;
    conditionLevel: string;
    category: string;
    images: string;
    viewCount: number;
  };
}

export function getItemDetail(id: number): Promise<ItemDetailResponse> {
  return request.get(`/api/item/detail/${id}`);
}

/**
 * 更新商品信息
 */
export interface UpdateItemParams {
  title?: string;
  description?: string;
  price?: number;
  conditionLevel?: string;
  category?: string;
  images?: string;
  viewCount?: number;
}

export interface UpdateItemResponse {
  success: boolean;
  message: string;
  data?: {
    id: number;
    userId: number;
    title: string;
    description: string;
    price: number;
    conditionLevel: string;
    category: string;
    images: string;
    viewCount: number;
    status: string;
  };
}

export function updateItem(id: number, params: UpdateItemParams): Promise<UpdateItemResponse> {
  return request.put(`/api/item/update/${id}`, params);
}

/**
 * 删除商品
 */
export interface DeleteItemResponse {
  success: boolean;
  message: string;
}

export function deleteItem(id: number): Promise<DeleteItemResponse> {
  return request.delete(`/api/item/delete/${id}`);
}

/**
 * 上传商品图片
 */
export interface UploadImageResponse {
  success: boolean;
  message: string;
  data?: {
    url: string;
    filename: string;
    size: number;
  };
}

export function uploadItemImage(file: File, userId: number): Promise<UploadImageResponse> {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('userId', userId.toString());
  
  return request.post('/api/item/upload-image', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

/**
 * 获取我的商品列表（带搜索、筛选和分页）
 */
export interface GetMyItemsListParams {
  userId: number;
  keyword?: string;
  status?: string;
  page?: number;
  size?: number;
}

export function getMyItemsList(params?: GetMyItemsListParams): Promise<UserItemsResponse> {
  return request.get('/api/item/my-items', {
    params: {
      userId: params?.userId,
      keyword: params?.keyword,
      status: params?.status,
      page: params?.page || 1,
      size: params?.size || 10
    }
  });
}

/**
 * 更新商品状态
 */
export interface UpdateItemStatusParams {
  id: number;
  status: string;
}

export interface UpdateItemStatusResponse {
  success: boolean;
  message: string;
}

export function updateItemStatus(id: number, status: string): Promise<UpdateItemStatusResponse> {
  return request.put('/api/item/update-status', {
    id,
    status
  });
}
